﻿using MQ.Core.Cache;
using MQ.Core.Common;
using MQ.Core.Entity.System;
using MQ.Core.IRepository.System;
using MQ.Core.IService.System;
using MQ.Core.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MQ.Core.Service.System
{
    public class DepartmentService : BaseService, IDepartmentService
    {
        private readonly IDepartmentRepository departmentRepository;
        private const string CACHE_ID = "SYS:DEPARTMENT:";
        public DepartmentService(IDepartmentRepository departmentRepository):base(departmentRepository)
        {
            this.departmentRepository = departmentRepository;
        }
        [Cache(CACHE_ID + "TREE", false)]
        public override void Delete(string ids)
        {
            departmentRepository.Delete(ids);
        }
        [Cache(CACHE_ID+"TREE",false)]
        public override Task DeleteAsync(string ids)
        {
            return departmentRepository.DeleteAsync(ids);
        }

        public override IEnumerable<DepartmentEntity> GetList<DepartmentEntity>(string query)
        {
            return departmentRepository.GetList<DepartmentEntity>(query);
        }

        public override Task<IEnumerable<T>> GetListAsync<T>(string query)
        {
            throw new NotImplementedException();
        }

        public override IEnumerable<T> GetPageList<T>(Pagination pagination, string query)
        {
            throw new NotImplementedException();
        }

        public override Task<IEnumerable<T>> GetPageListAsync<T>(Pagination pagination, string query)
        {
            throw new NotImplementedException();
        }
        [Cache(CACHE_ID+"TREE",false)]
        public IEnumerable<TreeModel> GetTree(int companyId)
        {
            var list = GetList<DepartmentEntity>($"{{\"companyId\":\"{companyId}\"}}");
            var treelist = new List<TreeModel>();
            foreach(var item in list)
            {
                treelist.Add(new TreeModel() {
                    id=item.Id,
                    pid=item.ParentId,
                    text=item.Name,
                    value=item.Id.ToString()
                });
            }
            return treelist.ToTree();
        }
    }
}
